
var validator = {};

/**
 * @description: 判断是否为空。
 * 自动匹配字符串、数组、对象
 * @param {object} :需要判断为空的数据
 * @return {boolean} ：返回是否为空：true\false
 */
validator.isNull = function (obj) {
    // if(obj =='' || obj == null || obj == undefined){
    //     return true;
    // }
    if (Object.prototype.toString.call(obj) === '[object Boolean]') {
        return false;
    }else if (Object.prototype.toString.call(obj) === '[object Array]') {
        try {
            if (obj.length == 0) { // "",[]
                return true;
            }else{
                return false;
            } 
        } catch (error) {
            return true;
        }
    }else if (Object.prototype.toString.call(obj) === '[object Object]') {
        try {
            if(Object.keys(obj).length <=0){
                return true;
            } else{
                return false;
            }              
        } catch (error) {
            return true;
        }
    }else if (Object.prototype.toString.call(obj) === '[object String]'){
        try {
            if (obj.trim() == '' || obj.trim().toLocaleLowerCase() == 'null' || obj.trim().toLocaleLowerCase() == 'undefined') {            
                return true;
            }else{
                return false;
            }
        } catch (error) {        
            return true;
        }
    }else if (Object.prototype.toString.call(obj) === '[object Null]'){
        return true;
    }else if (Object.prototype.toString.call(obj) === '[object Undefined]'){
        return true;
    }else if (Object.prototype.toString.call(obj) === '[object Number]'){
        if(obj.toString() == 'NaN'){
            return true;
        }else{
            return false;
        }
    }else if (Object.prototype.toString.call(obj) === '[object Error]'){
        if(obj.toString().trim().length<=0){
            return true;
        }else{
            return false;
        }
    }else{
        try {
            if (obj == null || obj == undefined) {           
                return true;
            }
        } catch (error) {
            return true;
        }
    
        try {
            if (obj === undefined) {            
                return true;
            }
        } catch (error) {        
            return true;
        }    
        try {
            if (obj.trim() == '') {            
                return true;
            }
        } catch (error) {        
            return true;
        }  
    
        try {
            // "",null,undefined,NaN
            if (!obj) {
                return true;
            }
        } catch (error) {
            return true;
        }
    }   
    return false;

}


/**
 * 是否包含中文
 * @param {string} s 原始字符
 */
validator.isContainChinese = function (s) {
    var patrn = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;
    if (!patrn.exec(s)) {
        return false;
    }
    else {
        return true;
    }
}

/**
 * 是否是英文
 * @param {string} s 原始字符
 */
validator.isEN = function (s) {
    var re = new RegExp("[a-zA-Z]");
    if (re.test(s)) { 
        return true; 
    }else { 
        return false; 
    }
}

/**
 * @description: 判断是否是纯数字
 * @param {object} 
 * @return {bool}
 */
validator.isNum = function (s) {
    if (!this.isNull(s)) {
        return !isNaN(s);
    }
    return false;
};


/**
 * @description: 数据正则检测
 * @param {string } reg 原字符串，正则表达式
 * @returns {bool} 验证结果
 */
validator.regCheck = function (str, reg) {
    if (this.isNull(str)) { //输入不能为空
        return false;
    } else if (!reg.test(str)) { //正则验证不通过，格式不对
        console.log("正则验证不通过!");
        return false;
    } else {
        console.log("正则验证通过！");
        return true;
    }
}

/**
 * @description: 邮箱格式检测
 * @param {string} email 邮箱字符串
 * @returns {bool} 邮箱格式是否正确
 */
validator.checkEmail = function (email) {
    var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //正则表达式
    return this.regCheck(email, reg);
}


/**
 * 检查字符串是否合法。只能包含中文、英文、下划线
 * @param {string} str 
 */
validator.checkStr = function (str) {
    var reg = new RegExp("^[一-龥A-Za-z0-9_]+$"); //正则表达式
    return this.regCheck(str, reg);
}


/**
 * 检查密码，只包含中文、英文、下划线 ，且长度在6-18之间
 * @param {string} str 
 */
validator.checkPwd = function (str) {
    var reg = new RegExp("^[A-Za-z0-9_]{6,18}$"); //正则表达式
    return this.regCheck(str, reg);
}

/**
 * 验证手机号
 * @param {string} str 
 */
validator.checkPhoneNum = function (str) {
    var reg = new RegExp("^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$"); //正则表达式
    return this.regCheck(str, reg);
}

/**
 * 验证是否为整数,包括正整数负整数
 * @param {string} str 
 */
validator.isInteger = function (str) {
    var reg = new RegExp("^[-+]?\d*$"); //正则表达式
    return this.regCheck(str, reg);
}

/**
 * 验证是否为小数,包括正负数
 * @param {string} str 
 */
validator.isInteger = function (str) {
    var reg = new RegExp("^[-\+]?\d+(\.\d+)?$"); //正则表达式
    return this.regCheck(str, reg);
}

/**
 * 验证是否是中文
 * @param {string} str 
 */
validator.isChinese = function (str) {
    var reg = new RegExp("^[\u0391-\uFFE5]+$"); //正则表达式
    return this.regCheck(str, reg);
}

/**
 * 验证IP
 * @param {string} str 
 */
validator.checkIp = function (str) {
    var reg = new RegExp("^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"); //正则表达式
    return this.regCheck(str, reg);
}

/**
 * 验证url
 * @param {string} str 
 */
validator.checkUrl = function (str) {
    // var reg = new RegExp("^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$"); //正则表达式
    var reg = new RegExp("^((https|http|ftp|rtsp|mms)?://)"
                       + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" // ftp    的user@
                       + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
                       + "|" // 允许IP和DOMAIN（域名）
                       + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
                       + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
                       + "[a-z]{2,6})" // first level domain- .com or .museum
                       + "(:[0-9]{1,4})?" // 端口- :80
                       + "((/?)|" // a slash isn't required if there is no file name
                       + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$");
    return this.regCheck(str, reg);
}

/**
 * 验证身份证号。
 * @param {string} sId 身份证号
 * @returns {object} 
 */
validator.checkIdCard = function (sId) {
    var obj={};
    var aCity = {
        11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州"
        , 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外"
    }
    var iSum = 0
    var info = ""
    if (!/^\d{17}(\d|x)$/i.test(sId)) {
        return obj;
    }
    sId = sId.replace(/x$/i, "a");
    if (aCity[parseInt(sId.substr(0, 2))] == null) {
        // return "Error:非法地区";
        return obj;
    }
    sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
    var d = new Date(sBirthday.replace(/-/g, "/"))
    if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) {
        // return "Error:非法生日";
        return obj;
    }
    for (var i = 17; i >= 0; i--) {
        iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11)
    }
    obj={
        city:aCity[parseInt(sId.substr(0, 2))],
        birthday:sBirthday,
        sex:(sId.substr(16, 1) % 2 ? "男" : "女")
    }
    return obj;
    //return aCity[parseInt(sId.substr(0, 2))] + "," + sBirthday + "," + (sId.substr(16, 1) % 2 ? "男" : "女");
}

/**
 * 验证是否是有效时间
 */
validator.isTime = (s)=>{
    var reg = /^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/;
    var res = reg.test(s);
    return res
}
/**
 * 验证是否是有效日期
 */
validator.isDate = (s)=>{
    // var reg = /^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/;
    var reg =/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
    var res = reg.test(s);
    return res
}



module.exports = validator;